Repository navigation
Conversation
✅ Deploy Preview for stoplight-elements ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for stoplight-elements-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
drewzhao
force-pushed
the
fix-hash-router-navigation-in-web-components
branch
from
January 7, 2026 14:01
d7eced4 to
9c10648
Compare
|
Hey folks, my team is stuck on version 8.5.2 because of the issue this PR solves. Any reason why it wasn't merged yet, considering it seems ready to go? |
…mode React Router v6's hash history listens to `popstate` only. Host routers that change the URL with `history.pushState()` and then dispatch `hashchange` never fire `popstate`, so the router keeps the old route while the URL shows the new one. VitePress does this for every same-page link it intercepts: it calls `preventDefault()`, so React Router's `<Link>` skips its own navigation, then pushes the URL and dispatches a synthetic `HashChangeEvent`. Elements v8 worked because the history v4 hash history also listened to `hashchange`. Back and Forward are unaffected, because a real history traversal fires `popstate`. Add HashRouterSync, rendered next to the HashRouter that withRouter creates for `router="hash"`. On `hashchange` it moves the router to the new hash with `replace`, because the host already created the history entry. It: - acts only on hashes that are routes of this router (`#/...` under `basePath`, or an empty hash for the root), so a host page's own fragments, such as a skip link (`#main-content`), are left alone; - strips `basePath` before calling `navigate()`, which adds it again; - does nothing when the router is already at that location, for example after a native fragment navigation that React Router handled via `popstate`. Fixes stoplightio#2792 Co-authored-by: Drew Zhao <drewzhao@outlook.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
drewzhao
force-pushed
the
fix-hash-router-navigation-in-web-components
branch
from
November 14, 2026 14:57
bedf975 to
5bb7ee1
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
router="hash", Elements stops following navigation when it runs inside a page that has its own client-side router, such as a VitePress site. Clicking an operation in the table of contents updates the URL hash, but Elements keeps showing the previous page. Back and Forward still work.This worked in 8.5.2, broke in 9.0.0 with the move to React Router 6 (8520585), and still happens in 9.0.29. Elements on a standalone page isn't affected.
Fixes #2792
Why it happens
React Router 6's hash history listens only for
popstate. That's enough for ordinary hash links, because the browser firespopstatewhen it follows one.Host routers often handle same-page links themselves. VitePress, for example, intercepts the click, calls
preventDefault(), updates the URL withhistory.pushState(), and then dispatches ahashchangeevent (click handler, hash update). Because the click is alreadydefaultPrevented, React Router's<Link>doesn't navigate either, andpushState()doesn't firepopstate. The router never sees the new hash.Elements 8 used React Router 5, whose hash history also listened for
hashchange. That's why this used to work.What this PR changes
withRouternow renders a small internal component,HashRouterSync, next to theHashRouterit creates forrouter="hash". When ahashchangeevent arrives, it moves the router to the new hash:#/…underbasePath, or an empty hash for the root. Other anchors on the host page, such as a skip link to#main-content, are left alone.basePathbefore callingnavigate(), which adds it back.replace, because the host has already added the history entry.There's no API change and nothing new is exported. Other router types, and apps that render Elements inside their own router, aren't affected.
How to reproduce
Save this page and serve it locally, for example with
python3 -m http.server. The script handles same-page hash links the way VitePress does.With the published bundle (9.0.29), clicking an operation such as "List sub accounts" changes the URL to
#/operations/accounts, but the page still shows the "Zoom API" overview. Point the first<script>at a build of this branch (packages/elements/dist/web-components.min.js) and the content follows each click.Testing
HashRouterSync.spec.tsxadds 25 tests. They cover following a route hash, ignoring other anchors, an empty hash, query strings and in-page anchors,basePath,replace, no extra navigation afterpopstate, cleanup on unmount, and thewithRouterwiring. One test shows that React Router alone ignores this kind of navigation. Reverting any of these behaviors in the implementation makes at least one test fail.elements-core(415 tests),elements(50) andelements-dev-portal(18) pass.tsc --noEmitand ESLint are clean.withRouterchange, a build of this branch is byte-identical to the published 9.0.29web-components.min.js. With it, the bundle is 936 bytes larger.#main-content, with the defaultbasePathand withbasePath="/docs":#VPContentin VitePress,#main-content)Out of scope
On a standalone page, the browser follows a host anchor such as
#main-contentitself and firespopstate, so React Router treats it as an unknown route. Elements then shows the first page with the defaultbasePath, and nothing with a non-rootbasePath. This PR doesn't change that.